import React from "react"

import { PageType } from "../business/types"
import http from "../helper/request"
import {
	IQueryRiskLevelDictListParams,
	IQueryRiskLevelListParams,
	IRiskLevelDict,
	IRiskLevelList,
	IRiskLevelState,
	IWindControlBlacklistResponse
} from "./types"

export const queryRiskLevelList = (params: IQueryRiskLevelListParams & PageType) =>
	http.post<IRiskLevelList>("/windControllerLevel/windControllerLevelList", params)
export const deleteRiskLevelState = (params: IRiskLevelState) =>
	http.post<any>("/windControllerLevel/windControllerLevelDelete", params)
export const queryRiskLevelDictList = (params: IQueryRiskLevelDictListParams) =>
	http.post<IRiskLevelDict[]>("/windControllerLevel/selectWindControlLevel", params)

const useApi = () => {
	const { useState } = React
	const [loading, setLoading] = useState(false)

	async function generalHttp<T>(url: string, body: object | undefined) {
		setLoading(true)
		const ret = await http.post<T>(url, body)
		setLoading(false)
		return ret
	}

	const updateRiskLevelState = async (params: IRiskLevelState) => {
		const ret = await generalHttp("/windControllerLevel/windControllerLevelUpdate", params)
		return ret
	}

	const addRiskLevelState = async (params: IRiskLevelState) => {
		const ret = await generalHttp("/windControllerLevel/windControllerLevelSave", params)
		return ret
	}

	const windControlBlacklist = async (body: object | undefined) => {
		const ret = await generalHttp<IWindControlBlacklistResponse>("/windControlBlacklist/query", body)
		return ret
	}

	const deleteWindControlBlackList = async (body: object | undefined) => {
		const ret = await generalHttp("/windControlBlacklist/del", body)
		return ret
	}

	const addWindControlBlackList = async (body: object | undefined) => {
		const ret = await generalHttp("/windControlBlacklist/add", body)
		return ret
	}

	return {
		windControlBlacklist,
		deleteWindControlBlackList,
		addWindControlBlackList,
		generalHttp,
		updateRiskLevelState,
		addRiskLevelState,
		loading
	}
}

export default useApi
